/******************************************************************************
 * Copyright (c) 2006, Intalio Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Intalio Inc. - initial API and implementation
 *******************************************************************************/

/**
 * Date             Author              Changes
 * Jul 12, 2006     hmalphettes         Created
 **/
package ms.unicorn.erml.figures;

import ms.unicorn.erml.edit.parts.SubPartEditPart;
import ms.unicorn.ui.layout.AnchorConstraint;
import ms.unicorn.ui.layout.DotNetLayout;
import ms.unicorn.ui.layout.DotNetLayoutConstraint;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.handles.HandleBounds;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

/**
 * Transparent figure
 * 
 * @author hmalphettes
 * @author <a href="http://www.intalio.com">&copy; Intalio, Inc.</a>
 */
public class SubPartFigure extends Shape
implements HandleBoundEx {

	public static final int COLLAPSE_HANDLE_HEIGHT = 30;
	protected Dimension corner = new Dimension(8, 8);
	
	private IFigure subpartPlate;
	
    public SubPartFigure(SubPartEditPart editpart) {
        //setBorder(new ms.unicorn.ui.borders.RoundedSchemeBorder());
        this.editPart = editpart;
        minSize = new Dimension(60, 80);
        
        setLayoutManager(new DotNetLayout());
       
        subpartPlate = new NodeFigure();
        subpartPlate.setLayoutManager(new ConstrainedToolbarLayout());
        
        add(subpartPlate, new AnchorConstraint(AnchorConstraint.ANCHOR_ALL, new Rectangle(), 
        		1, 1, 6, COLLAPSE_HANDLE_HEIGHT + 6));
    }
    
    private SubPartEditPart editPart;

    public IFigure getBodyCompartmentPlate() {
    	return subpartPlate;
    }
    
    /**
     * Calculates bounds of collapse handle in absolute coordinates according to
     * the figures bounds (zoom not computed == DP DevicePixel).
     * 
     * @generated not
     * @return
     */
    public Rectangle getAbsCollapseHandleBounds(boolean zoomCompute) {
        //mystery: why do I need a zoom here?
        double zoom = zoomCompute ? editPart.getZoom() : 1.0;
        double size = COLLAPSE_HANDLE_HEIGHT * zoom;//mm.LPtoDP(20);

        Rectangle theBounds = getBounds();
        PrecisionRectangle handleBounds = new PrecisionRectangle(theBounds);

        handleBounds.setX(handleBounds.x + (handleBounds.width - size) / 2);
        handleBounds.setY(handleBounds.y + (handleBounds.height - size));
        
        
        /*if (getPrimaryShape().getFigureSubProcessBorderFigure().hasChildren()) {
            handleBounds.setY(handleBounds.y - (ActivityEditPart.EVENT_FIGURE_SIZE*zoom)/2);
        }*/
        
        handleBounds.setWidth(size);
        handleBounds.setHeight(size);

        return handleBounds;
    }
    
    
    /**
     * Returns the Rectangle around which handles are to be placed.  The Rectangle should be
     * in the same coordinate system as the figure itself.
     * @return The rectangle used for handles
     */
    public Rectangle getHandleBounds() {
        Rectangle r = getBounds().getCopy().crop(new Insets(0, 0, 4, 4));        
        if (getParent() != null && (getParent().getParent() != null)) {
            int borderHeight = COLLAPSE_HANDLE_HEIGHT;
            /*for (Object child : getChildren()) {
                if (child instanceof SubProcessBorderFigure) {
                    borderHeight = ((SubProcessBorderFigure) child).getBorderHeight();
                    break;
                }
            }*/
            if (borderHeight != 0) {
                //r.height = (int) Math.floor(r.height - borderHeight/2);
                //the labels of the event shapes on the border might make the border bigger than one might think.
                r.height = r.height - borderHeight/2 + 4;
            }
        }
//        System.err.println("current height=" + getBounds().height);
        return r;
    }
    
    /**
     * @see Shape#fillShape(Graphics)
     */
    protected void fillShape(Graphics graphics) {
        Rectangle bound = getHandleBounds();
		graphics.fillRoundRectangle(bound, corner.width,
                corner.height);
    }
    
    /**
     * @see Shape#outlineShape(Graphics)
     */
    protected void outlineShape(Graphics graphics) {
        Rectangle f = Rectangle.SINGLETON;
        Rectangle r = getHandleBounds();
        f.x = r.x + lineWidth / 2;
        f.y = r.y + lineWidth / 2;
        f.width = r.width - lineWidth;
        f.height = r.height - lineWidth;
        graphics.drawRoundRectangle(f, corner.width, corner.height);
    }
    
    /**
     * Paints this Figure and its children.
     * Overridden to have the border paint before the children of the shape.
     * 
     * @param graphics The Graphics object used for painting
     * @see #paintFigure(Graphics)
     * @see #paintClientArea(Graphics)
     * @see #paintBorder(Graphics)
     */
    public void paint(Graphics graphics) {
        if (getLocalBackgroundColor() != null)
            graphics.setBackgroundColor(getLocalBackgroundColor());
        if (getLocalForegroundColor() != null)
            graphics.setForegroundColor(getLocalForegroundColor());
        Font font = getLocalFont();
        if (font != null)
            graphics.setFont(font);

        graphics.pushState();
        try {
            paintBorder(graphics);
            paintFigure(graphics);
            graphics.restoreState();
            paintClientArea(graphics);
            
            paintCollapseHandle(graphics);
            
        } finally {
            graphics.popState();
        }
    }
    
    public void paintBorder(Graphics graphics) {
        int alpha = 100;
        Color c = graphics.getBackgroundColor();
        int oriAlpha = graphics.getAlpha();
        graphics.setAlpha(alpha);
        graphics.setBackgroundColor(ColorConstants.black);

        Rectangle rect = getHandleBounds().getCopy().translate(3, 3);

        graphics.fillRoundRectangle(rect, corner.width, corner.height);
        
        graphics.setAlpha(oriAlpha);
        graphics.setBackgroundColor(c);
    }
        
    /**
     * Draws &quot;+&quot; for collapsed and &quot;-&quot; for expanded
     * subprocess.
     * 
     * @generated not
     */
    private void paintCollapseHandle(Graphics graphics) {
        Rectangle bounds = getAbsCollapseHandleBounds(false);
        int lineWidth = 1;
        
        //draw the chevon
        graphics.fillOval(bounds);
        graphics.drawOval(bounds);
        
        bounds.shrink(4, 4);
        //bounds.x += 2;
        //bounds.y += 2;
        graphics.setAlpha(45);
        graphics.setBackgroundColor(ColorConstants.black);
        graphics.fillOval(bounds);
        
        graphics.setAlpha(255);
        graphics.setForegroundColor(ColorConstants.black);
        graphics.setLineWidth(3);
        Point center = bounds.getCenter();
        
        PointList pts = new PointList(3);
        
        if (isCollapsed()) {
        	pts.addPoint(center.getTranslated(4, -3));
	        pts.addPoint(center.getTranslated(0, 3));
	        pts.addPoint(center.getTranslated(-4, -3));
        }
        else {
	        pts.addPoint(center.getTranslated(-4, 3));
	        pts.addPoint(center.getTranslated(0, -3));
	        pts.addPoint(center.getTranslated(4, 3));
        }
        graphics.drawPolyline(pts);
    }
    
    /**
     * 
     * @return true if the subprocess is collapsed.
     */
    public boolean isCollapsed() {
        return editPart.isCollapsed();
    }
}
